﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SelfManagement.Common;
using Ninject;
using SelfManagement.Web.Infrastructure;
using SelfManagement.Web.Models;
using SelfManagement.Web.Models.ViewModels;
using SelfManagement.Utility.Extensions;
using SelfManagement.IServices;

namespace SelfManagement.Web.Controllers
{
    [UserAuthorize]
    public class HomeController : CommonBaseController
    {

        [Inject]
        public IDailyService DailyService { get; set; }
        [Inject]
        public IArticleService ArticleService { get; set; }

        // GET: Home
        public ActionResult Index()
        {
            UserHomePageViewModel viewModel = new UserHomePageViewModel();

            viewModel.UserInfo = new UserInfoItem()
            {
                UserFacee = UserSession.FaceImg,
                UserName = UserSession.TrueName,
                Sex = UserSession.Sex.EnumDescription(),
                Identity = UserSession.UserEntity.Identity,
                PersonalInfo = UserSession.UserEntity.PersonalInfo,
            };

            DateTime today = DateTime.Now.Date;
            viewModel.NewestDailyItems = DailyService.QueryUserDailies(UserSession.Identity)
                                                     .OrderByDescending(d => today == d.Date)
                                                     .ThenByDescending(d => d.Date)
                                                     .Select(d => new NewestDailyItem()
                                                     {
                                                         DailyId = d.Id,
                                                         TaskName = d.TaskName,
                                                         Date = d.Date,
                                                         TasksCount = d.Tasks.Count(),
                                                         TaskStauts = d.Status
                                                     })
                                                     .Take(3)
                                                     .ToList();



            viewModel.NewestArticleItem = ArticleService.QueryUserArticles(UserSession.Identity)
                                                        .OrderByDescending(a => a.IsTop)
                                                         .OrderByDescending(a => a.UpdatedTime)
                                                         .Select(a => new NewestArticleItem()
                                                         {
                                                             ArticleId = a.Id,
                                                             CategoryName = a.Category.CategoryName,
                                                             Title = a.Title,
                                                             PublishTime = a.CreatedTime,
                                                             IsTop = a.IsTop,
                                                         })
                                                         .Take(9)
                                                         .ToList();

            SetPageTitle("主页");
            return View(viewModel);
        }
    }
}